﻿/********************************************************************************************
 * 文件名称:	DataSetReverse.cs
 * 设计人员:	(yanxianghui@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;

namespace Shanfree.Framework.Utility
{
    /// <summary>
    /// 泛型映射到DateTable的辅助类
    /// </summary>
    public class DataTableHandler<T>
    {
        /// <summary>
        /// 将IList泛型映射到行
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DataTable GetDateTableFromList(IList<T> list)
        {
            DataTable dt = new DataTable();
            Type type = typeof(T);
            PropertyInfo[] proList = type.GetProperties();
            foreach (PropertyInfo item in proList)
            {
                // 若非值类型
                if (item.PropertyType.IsByRef)
                {
                    continue;
                }
                string name = item.Name;
                dt.Columns.Add(name);
            }
            foreach (T t in list)
            {
                DataRow dr = dt.NewRow();
                foreach (PropertyInfo item in proList)
                {
                    // 若非值类型
                    if (item.PropertyType.IsByRef)
                    {
                        continue;
                    }
                    string name = item.Name;
                    object oValue = item.GetValue(t, null);
                    dr[name] = oValue;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        /// <summary>
        /// 将IList泛型映射到行或列
        /// </summary>
        /// <param name="list"></param>
        /// <param name="ObjectToColumn"></param>
        /// <returns></returns>
        public DataTable GetDateTableFromList(IList<T> list,bool ObjectToColumn)
        {
            if (ObjectToColumn == false)
            {
                return GetDateTableFromList(list);
            }
            DataTable dt = new DataTable();
            for (int i = 0;i < list.Count;i++)
            {
                T t = list[i];
                string name = "column" + i;
                dt.Columns.Add(name);
            }
            Type type = typeof(T);
            PropertyInfo[] proList = type.GetProperties();
            foreach (PropertyInfo item in proList)
            {
                // 若非值类型
                if (item.PropertyType.IsByRef)
                {
                    continue;
                }
                // 遍历列表中所有实例的同一属性,设置为行
                DataRow dr = dt.NewRow();
                for (int i = 0; i < list.Count; i++)
                {
                    T t = list[i];
                    string name = "column" + i;
                    object oValue = item.GetValue(t, null);
                    dr[name] = oValue;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }

}
